package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.qurey.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.ITravelStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ITravelCommentService travelCommentService;
    @Autowired
    private ITravelStatisVORedisService travelStatisVORedisService;


    @GetMapping("/query")
    private Object query( TravelQuery qo){
        return JsonResult.success(travelService.query(qo));
    }

    @GetMapping("/detail")
    private Object detail(String  id){
        return JsonResult.success(travelService.get(id));
    }

    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
    @PostMapping("/saveOrUpdate")
    @RequireLogin
    private Object saveOrUpdate(Travel travel,HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserInfoToken(token);
        travel.setUserId(user.getId());
        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }
    @GetMapping("/users")
    private Object users(@UserParam UserInfo userInfo){
        return JsonResult.success(userInfo);
    }

    @PostMapping("/commentAdd")
    @RequireLogin
    private Object commentAdd(TravelComment comment,@UserParam UserInfo userInfo){
        BeanUtils.copyProperties(userInfo,comment );
        comment.setUserId(userInfo.getId());
        travelCommentService.save(comment);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    private Object  comments(String travelId){
        return JsonResult.success(travelCommentService.queryByTravelId(travelId));
    }


    @PostMapping("/travelThumbup")
    @RequireLogin
    private Object  travelThumbup(String tid, @UserParam UserInfo userInfo){
        return JsonResult.success(travelStatisVORedisService.travelThumbup(tid,userInfo.getId()));
    }

    @GetMapping("/statisVo")
    private Object  statisVo(String tid){
        return JsonResult.success(travelStatisVORedisService.getTravelStatisVo(tid));
    }

}
